#include "ExampleScene.h"

#include <graphics/utils.h>
#include <render/renderer.h>

ExampleScene::ExampleScene()
    : Yam3DScene()
    , m_camera(0)
    , m_mesh(0)
    , m_program(0)
{
    // Create triangle with positions andf colors
    core::vector<math::vec3> positionData;
    positionData.push_back( math::vec3(  0.0f,  0.5f, 0.0f) );
    positionData.push_back( math::vec3( -1.0f, -0.5f, 0.0f) );
    positionData.push_back( math::vec3(  1.0f, -0.5f, 0.0f) );
    graphics::VertexArray* positions = new graphics::VertexArray(positionData);

    core::vector<math::vec3> colorData;
    colorData.push_back( math::vec3(  0.0f, 0.0f, 1.0f) );
    colorData.push_back( math::vec3(  0.0f, 1.0f, 0.0f) );
    colorData.push_back( math::vec3(  1.0f, 0.0f, 0.0f) );
    graphics::VertexArray* colors = new graphics::VertexArray(colorData);

    graphics::VertexBuffer* vb = new graphics::VertexBuffer();
    vb->addVertexArray("a_position",positions);
    vb->addVertexArray("a_color",colors);

    core::vector<unsigned int> indices;
    indices.push_back(0);
    indices.push_back(1);
    indices.push_back(2);
    graphics::IndexBuffer* ib = new graphics::IndexBuffer(indices);

    m_mesh = new graphics::Mesh(vb,ib);

    // Create cool vertex and fragment shaders.
    render::Shader *vshader = new render::Shader(render::Shader::Vertex);
    char vShaderStr[] =
       "uniform mat4 matModelViewProj;                   \n"
       "attribute vec4 a_position;                       \n"
       "attribute vec4 a_color;                          \n"
       "varying vec4    v_outVColor;                     \n"
       "void main()                                      \n"
       "{                                                \n"
       "   gl_Position = matModelViewProj * a_position;  \n"
       "   v_outVColor = a_color;                        \n"
       "}                                                \n";
    vshader->compileSourceCode(vShaderStr);

    render::Shader *fshader = new render::Shader(render::Shader::Fragment);
    char fShaderStr[] =
       "varying vec4    v_outVColor;                     \n"
       "void main()                                      \n"
       "{                                                \n"
       "  gl_FragColor = v_outVColor;                    \n"
       "}                                                \n";
    fshader->compileSourceCode(fShaderStr);

    m_program = new render::ShaderProgram();
    m_program->addShader(vshader);
    m_program->addShader(fshader);
    m_program->link();
    m_program->bind();

    // Create camera for our scene.
    m_camera = new graphics::Camera();

 //   m_pass.setCamera(m_camera);
    m_pass.setShaderProgram(m_program);
}

ExampleScene::~ExampleScene()
{
}

void ExampleScene::resize(int width, int height)
{
    m_camera->setPerspective(60.0f, float(width)/float(height), 1.0f, 1000.0f);
   glViewport( 0,0, width, height);
}

void ExampleScene::update(float deltaTime)
{
    /*m_camera->setTranslation( math::vec3(0,0,5) );

    m_mesh->setTranslation( math::vec3(0,0,0) );
    m_mesh->setOrientation( m_mesh->getOrientation()* math::quat::fromAxisAndAngle(0,1,0,180*deltaTime) );*/
}

void ExampleScene::render() const
{
    render::Renderer renderer;

    //renderer.setViewport( );
    renderer.applyPass(&m_pass);

    // Render mesh (sets attributes and draws the object)
    renderer.render(m_mesh, m_meshTransform);
}


